package com.wechat;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * TCP客户端
 */
public class TcpClientDemo01 {
    public static void main(String[] args) {
        // 1.连接服务器Socket 端口
        Socket socket = null;
        OutputStream os = null;
        try {
            // 服务器的地址 端口号
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            socket = new Socket(serverIP, port);
            // 发送消息
            os = socket.getOutputStream();
            // 2.发送消息
            os.write("开工大吉，新的一年，好好赚钱".getBytes());
            System.out.println();
            os.write("2022年财源广进".getBytes());
            System.out.println();
            os.write("顺顺~利利".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
